package net.zjitc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.zjitc.mapper.CategoryMapper;
import net.zjitc.mapper.OrderMapper;
import net.zjitc.mapper.ProductMapper;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.Product;
import net.zjitc.service.ProductService;
import net.zjitc.vo.PageBean;
import net.zjitc.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @AUTHOR zhj
 * @DATE 2019/3/29 12:30
 */
@Service
@Transactional
public class ProductServiceImpl implements ProductService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    OrderMapper orderMapper;

    @Override
    public List<Product> findAll() {
        return productMapper.findAll();
    }

    @Override
    public Product findById(String id) {
        return productMapper.findById(id);
    }

    @Override
    public int addProduct(Product product) {
        return productMapper.addProduct(product);
    }

    @Override
    public int editProduct(Product product) {
        return productMapper.editProduct(product);
    }

    @Override
    public int deleteProduct(String id) {
        return productMapper.deleteProduct(id);
    }


    @Override
    public List<Product> searchByCondition(ProductVo vo) {
        List<Product> list = productMapper.searchByCondition(vo);
        for (Product product :list){
            Category category = categoryMapper.findById(product.getCid());
            product.setCategory(category);
        }
        return list;
    }

    /**
     * 按书名查询，并以分页显示
     * @param vo
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageBean<Product> serachByNameToPage(ProductVo vo, Integer currentPage, Integer pageSize) {
        if (currentPage == null){
            currentPage = 1;
        }
        PageBean pageBean = new PageBean<>();
        PageHelper.startPage(currentPage,pageSize);
        List<Product> productList = productMapper.searchByCondition(vo);
        PageInfo<Product>info = new PageInfo(productList);
        pageBean.setData(info);
        return pageBean;
    }


    @Override
    public List<Product> findByCid(Integer cid) {

        if (cid == null){
            return productMapper.findAll();
        }

        return  productMapper.findByCid(cid);


    }

    @Override
    public PageBean<Product> findPageProduct(Integer cid, int currentPage, int pageSize) {
        //查询所有
       //查询所有的内容


        PageBean pageBean = new PageBean();
        if (cid == null){
            PageHelper.startPage(currentPage,pageSize);
            List<Product> productList = productMapper.findAll();
            PageInfo info = new PageInfo(productList);
            pageBean.setMessage("全部商品目录");
            pageBean.setData(info);
           return pageBean;
        }

        // 根据cid查询分类的名称
        PageHelper.startPage(currentPage,pageSize);
        List<Product> productList = productMapper.findByCid(cid);
        PageInfo<Product> info = new PageInfo<>(productList);
        pageBean.setData(info);
        Category category = categoryMapper.findById(cid);
        pageBean.setMessage(category.getName());
        return pageBean;

    }





}
